/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "SmartRouteEngine.h"

CSmartRouteEngine::CSmartRouteEngine() :
    iState(ERouteIdle)
	{
	}


CSmartRouteEngine::~CSmartRouteEngine()
	{
	delete iRoute;
	delete iLocator;
	delete iDataStore;
	}

CSmartRouteEngine* CSmartRouteEngine::NewLC()
    {
	CSmartRouteEngine* self = new (ELeave)CSmartRouteEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
    }

CSmartRouteEngine* CSmartRouteEngine::NewL()
    {
	CSmartRouteEngine* self=CSmartRouteEngine::NewLC();
	CleanupStack::Pop(); // self;
	return self;
    }

void CSmartRouteEngine::ConstructL()
	{
	iDataStore = CSmartRouteDataStore::NewL();
	iLocator = CSmartRouteLocator::NewL(*this);
	}

	
void CSmartRouteEngine::StartL(TInt aFrequency)
	{
	iFrequency = aFrequency;
	delete iRoute; 
	iRoute = NULL;
	iRoute = CSmartRoute::NewL();
	
	iLapStart.HomeTime();
	iRoute->SetStart(iLapStart);
	iDuration =  0;
	
	iLocator->StartL(aFrequency);
	iState = ERouteRecording;
	}

void CSmartRouteEngine::Stop()
	{
	iLocator->Cancel();
	iState = ERouteIdle;
	
	TTime now; 
	now.HomeTime();
	iRoute->SetEnd(now);
	
	TTimeIntervalSeconds lapDuration;
	User::LeaveIfError(now.SecondsFrom(iLapStart, lapDuration));
	
	iDuration += lapDuration.Int();
	iRoute->SetDuration(iDuration);
	iDataStore->SaveRouteL(*iRoute);
	}
	
void CSmartRouteEngine::Pause()
	{
	TTime now; 
	now.HomeTime();
	TTimeIntervalSeconds lapDuration;
	User::LeaveIfError(now.SecondsFrom(iLapStart, lapDuration));
	iDuration += lapDuration.Int();
	
	iLocator->Pause();
	iState = ERoutePaused;
	}

void CSmartRouteEngine::Resume()
	{
	iLapStart.HomeTime();
	iLocator->StartL(iFrequency);
	iState = ERouteRecording;
	}
	
TSmartRouteState CSmartRouteEngine::State() const
    {
    return iState;
    }
    
 void CSmartRouteEngine::AddRoutePointL(const TRoutePoint& aPoint)
 	{
	__ASSERT_ALWAYS(iRoute, Panic(ESmartRouteIllegalRoute));
 	iRoute->AddPointL(aPoint);
 	}
 	
 void CSmartRouteEngine::ListRoutesL(RShortRouteArray& aRouteList)
 	{
 	iDataStore->ListRoutesL(aRouteList);
 	}

CSmartRoute* CSmartRouteEngine::RestoreRouteL(const TUint32& aId)
	{
	return iDataStore->RestoreRouteL(aId);
	}

void CSmartRouteEngine::DeleteRouteL(const TUint32& aId)
	{
	iDataStore->DeleteRouteL(aId);
	}